﻿using MiniJob.Entity;

namespace MiniJob.Service;

public interface IJobService : IBaseService<JobInfo>
{
    /// <summary>
    /// 查询条件：任务开启 + 使用CRON表达调度时间 + 指定appId + 即将需要调度执行
    /// </summary>
    /// <param name="appId"></param>
    /// <param name="time"></param>
    /// <returns></returns>
    Task<List<JobInfo>> FindTriggerJobs(Guid appId, long time);
}

public class JobService(IFreeSql freeSql) : BaseService<JobInfo>(freeSql), IJobService
{
    public Task<List<JobInfo>> FindTriggerJobs(Guid appId, long time)
    {
        return FreeSql.Select<JobInfo>()
            .Where(p => p.AppId == appId && p.Enable && p.NextTriggerTime <= time && p.TimeExpressionType == Jobs.TimeExpressionType.Cron)
            .ToListAsync();
    }
}
